package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.exception.ServiceException;
import com.share.common.core.utils.StringUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Region;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.mapper.CabinetMapper;
import com.share.device.mapper.RegionMapper;
import com.share.device.mapper.StationMapper;
import com.share.device.repository.StationLocationRepository;
import com.share.device.service.ICabinetService;
import com.share.device.service.IRegionService;
import com.share.device.service.IStationService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private CabinetMapper cabinetMapper;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private StationLocationRepository stationLocationRepository;

    //分页查询站点列表
    @Override
    public List<Station> selectStationList(Station station) {
        List<Station> stationList = stationMapper.selectStationList(station);

        if(stationList.isEmpty()) throw new ServiceException("无站点");

        //冗余字段cabinetNo赋值 为每个站点的柜机编号赋值
        for (Station st : stationList) {
            Long cabinetId = st.getCabinetId();
            //mp根据id查询柜机
            Cabinet cabinet = cabinetMapper.selectById(cabinetId);

            st.setCabinetNo(cabinet.getCabinetNo());
        }

        return stationList;
    }

    @Override
    public int saveStation(Station station) {
        //根据地区编号获取地区名称，并且设置完整地址地段
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());
        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());

        //在mysql中新增站信息后，同时将经纬度信息存入MongoDB中
        StationLocation stationLocation = new StationLocation();
        stationLocation.setId(ObjectId.get().toString()); //生成id
        stationLocation.setStationId(station.getId()); //与对应站点绑定
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(),station.getLatitude().doubleValue())); //经纬度
        stationLocation.setCreateTime(new Date()); //时间
        stationLocationRepository.save(stationLocation);

        return stationMapper.insert(station);
    }

    @Override
    public int updateStation(Station station) {
        //根据地区编号获取地区名称，并且设置完整地址地段
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());
        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());

        //更新站点信息后，同时在MongoDB中的经纬度信息进行修改
        StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocationRepository.save(stationLocation);

        return stationMapper.updateById(station);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        //使用mp批量删除
        int rows = stationMapper.deleteBatchIds(ids);

        //同时将MongoDB中的数据删除
        for (Long id : ids) {
            stationLocationRepository.deleteByStationId(id);
        }

        return rows;
    }

    //启用站点对应的柜机
    @Override
    public int setData(Station station) {
        updateById(station);

        //启用柜机 状态设置为1
        Cabinet cabinet = cabinetService.getById(station.getCabinetId());
        cabinet.setStatus("1");
        cabinetService.updateById(cabinet);
        return 1;
    }

    @Override
    public void updateData() {
        //获取mysql中所有的站点数据
        List<Station> stationList = list();

        //遍历所有站点
        for (Station station : stationList) {
            //在MongoDB中查询是否有当前站点的数据
            StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());

            //如果没有，则进行同步
            if(stationLocation == null) {
                stationLocation = new StationLocation();
                stationLocation.setId(ObjectId.get().toString());
                stationLocation.setStationId(station.getId());

                //坐标信息
                stationLocation.setLocation(new GeoJsonPoint(
                        station.getLongitude().doubleValue(),
                        station.getLatitude().doubleValue()));

                stationLocation.setCreateTime(new Date());
                stationLocationRepository.save(stationLocation);
            }

        }
    }

}
